﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Common;
using System.Data;
using TRPOP.Common;
using TRPOP.Config;
using TRPOP.CMS.Message.model;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Message.DataAccess
{
    public class MessageReplyDAL
    {

        private static TRPOP.ComSqlDAL.ICommonDAL CommonDAL = TRPOP.CMS.Plugin.ICommonDALAccess.GetInstance();
        #region 私有的方法
        ///	<summary>
        ///	从Table取数据到实体
        ///	</summary>
        ///	<param name="oDataRow">数据行</param>
        ///	<returns>返回实体</returns>
        private static MessageReplyInfo ParseRowTomodel(DataRow oDataRow)
        {
            MessageReplyInfo oInfo = new MessageReplyInfo();
            oInfo.iMessageID = oDataRow["iMessageID"] is DBNull ? 0 : (int)oDataRow["iMessageID"];
            oInfo.ntMessageReplyContent = oDataRow["ntMessageReplyContent"] is DBNull ? "" : (string)oDataRow["ntMessageReplyContent"];
            oInfo.dtMessageReplyTime = oDataRow["dtMessageReplyTime"] is DBNull ? (new DateTime(1900, 1, 1)) : (DateTime)oDataRow["dtMessageReplyTime"];
            oInfo.nvcMessageReplyUserName = oDataRow["nvcMessageReplyUserName"] is DBNull ? "" : (string)oDataRow["nvcMessageReplyUserName"];
            oInfo.nvcMessageReplyTitle = oDataRow["nvcMessageReplyTitle"] is DBNull ? "" : (string)oDataRow["nvcMessageReplyTitle"];
            oInfo.iDisplayOrder = oDataRow["iDisplayOrder"] is DBNull ? 0 : (int)oDataRow["iDisplayOrder"];
            oInfo.ntExtendProperty = oDataRow["ntExtendProperty"] is DBNull ? "" : (string)oDataRow["ntExtendProperty"];
            oInfo.iMessageReplyID = oDataRow["iMessageReplyID"] is DBNull ? 0 : (int)oDataRow["iMessageReplyID"];
            return oInfo;
        }


        ///	<summary>
        ///	通用的添加存储过程的变量
        ///	</summary>
        ///	<param name="osdDataBase">SqlDatabase</param>
        ///	<param name="odbCommand">DbCommand</param>
        ///	<param name="oInfo">实体</param>
        private static MessageReplyInfo ParseModeltoParam(SqlDatabase odbDataBase, DbCommand odbCommand, MessageReplyInfo oInfo)
        {
            odbDataBase.AddInParameter(odbCommand, "@iMessageID", System.Data.DbType.Int32, oInfo.iMessageID);
            odbDataBase.AddInParameter(odbCommand, "@ntMessageReplyContent", System.Data.DbType.String, oInfo.ntMessageReplyContent);
            odbDataBase.AddInParameter(odbCommand, "@dtMessageReplyTime", System.Data.DbType.DateTime, oInfo.dtMessageReplyTime);
            odbDataBase.AddInParameter(odbCommand, "@nvcMessageReplyUserName", System.Data.DbType.String, oInfo.nvcMessageReplyUserName);
            odbDataBase.AddInParameter(odbCommand, "@nvcMessageReplyTitle", System.Data.DbType.String, oInfo.nvcMessageReplyTitle);
            odbDataBase.AddInParameter(odbCommand, "@iDisplayOrder", System.Data.DbType.Int32, oInfo.iDisplayOrder);
            odbDataBase.AddInParameter(odbCommand, "@ntExtendProperty", System.Data.DbType.String, oInfo.ntExtendProperty);
            odbDataBase.AddParameter(odbCommand, "@iMessageReplyID", System.Data.DbType.Int32, ParameterDirection.InputOutput, null, DataRowVersion.Default, oInfo.iMessageReplyID);
            return oInfo;
        }


        #endregion

        ///	<summary>
        ///	 更新或添加
        ///	</summary>
        ///	<param name="oInfo"></param>
        ///	<returns>bool</returns>
        public static bool Save(MessageReplyInfo oInfo)
        {
            bool flag = false;
            try
            {
                SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                DbCommand odbCommand = odbDataBase.GetStoredProcCommand("MessageReply_Save");
                ParseModeltoParam(odbDataBase, odbCommand, oInfo);
                odbDataBase.ExecuteNonQuery(odbCommand);
                oInfo.iMessageReplyID = (int)odbDataBase.GetParameterValue(odbCommand, "@iMessageReplyID");
                flag = true;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return flag;
        }

        ///	<summary>
        ///	根据Primary Key获取信息实体
        ///	</summary>
        ///	<param name="intiMessageReplyID"></param>
        ///	<returns>MessageReplyInfo</returns>
        public static MessageReplyInfo GetInfoById(int intiMessageReplyID)
        {
            MessageReplyInfo oInfo = null;
            string strSqlString = "SELECT * FROM MessageReply WHERE  iMessageReplyID=" + intiMessageReplyID + "    ";
            try
            {
                DataSet oDataSet = CommonDAL.GetDataSet(strSqlString, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                DataTable oDataTable = oDataSet.Tables[0];
                if (oDataTable.Rows.Count == 1)
                {
                    oInfo = ParseRowTomodel(oDataTable.Rows[0]);
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oInfo;
        }

        ///	<summary>
        ///	 根据Primary Key删除信息实体
        ///	</summary>
        ///	<param name="intiMessageReplyID"></param>
        ///	<returns>bool</returns>
        public static bool DeleteInfoById(int intiMessageReplyID)
        {
            string strSqlString = "DELETE * FROM MessageReply WHERE   iMessageReplyID=" + intiMessageReplyID + "    ";
            try
            {
                return CommonDAL.ExeSQLQuery(strSqlString, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection) == Common.EnumSubmitResult.Success;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
        }

        ///	<summary>
        ///	 获取信息列表（无分页）
        ///	</summary>
        ///	<param name="intTopn">前N条，为0时表示所有符合条件的</param>
        ///	<param name="strCondition">自定义条件，要以And开头</param>
        ///	<param name="strOrderby">排序，不需要Order by </param>
        ///	<returns>List<MessageReplyInfo></returns>
        public static List<MessageReplyInfo> GetListByCondition(int intTopn, string strCondition, string strOrderby)
        {
            List<MessageReplyInfo> oList = new List<MessageReplyInfo>();
            string strSql = CommonDAL.strbuildSqlForList(intTopn, strCondition, strOrderby, "MessageReply");
            try
            {
                DataSet oDataSet = CommonDAL.GetDataSet(strSql, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                DataTable oDataTable = oDataSet.Tables[0];
                foreach (DataRow otempRow in oDataTable.Rows)
                {
                    oList.Add(ParseRowTomodel(otempRow));
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oList;
        }

        ///	<summary>
        ///	获取分页信息
        ///	</summary>
        /// <param name="intPageNumber">当前页页码</param>
        /// <param name="intPageSize">分页大小</param>
        /// <param name="strCondition">查询条件 and ....</param>
        /// <param name="strOrderby">排序的字段名 id desc</param>
        /// <param name="intRecordCount">总记录数</param>
        ///	<returns>List<MessageReplyInfo></returns>
        public static List<MessageReplyInfo> GetListByPager(int intPageNumber, int intPageSize, string strCondition, string strOrderby, out int intRecordCount)
        {
            List<MessageReplyInfo> oList = new List<MessageReplyInfo>();
            try
            {
                DataSet oDataSet = CommonDAL.GetListByPager("MessageReply", "*", intPageNumber, intPageSize, strCondition, strOrderby, out  intRecordCount, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                DataTable oDataTable = oDataSet.Tables[0];
                foreach (DataRow otempRow in oDataTable.Rows)
                {
                    oList.Add(ParseRowTomodel(otempRow));
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oList;
        }


    }

}
